<template lang="pug">
Select(
  v-bind="select"
  :onSelect="(option) => onSelect(option)",
  v-model="model"
  ref="selectRef"
)

</template>

<script setup lang="ts">
import { type SelectProps, Select } from "@scmt/common-components";
import { useApi, type ApiBasedOption } from "@scmt/core-api";
import type { DiagnosisCatalogEntity } from "@scmt/api-bdm-diagnosis-catalog";
import { useRequest } from "@scmt/common-hooks";
//添加事件
const emit = defineEmits(["select"]);

const selectRef = ref<null | InstanceType<typeof Select>>(null);
const $api = useApi();
const model = ref("");

const { request, data } = useRequest({
  westernMedicine: async (key: string) => {
    return $api.lis.specimen.listAsOptions(key);
  },
});
const query = (val: string) => {
  if (val) {
    request("westernMedicine", val);
  }
};
const select = computed<SelectProps>(() => {
  return {
    placeholder: "业务编码/标本条码",
    filterable: true,
    options: data.westernMedicine ?? [],
    modelValue: model.value,
    tableColumns: [
      {
        prop: "outpatientNumber",
        label: "业务编号",
        width: 150,
      },
      {
        prop: "barcode",
        label: "标本条码",
        width: 150,
      },
      {
        prop: "applicationProject",
        label: "检查项目",
      },
    ],
  };
});
const onSelect = (option: ApiBasedOption<DiagnosisCatalogEntity>) => {
  if (option && option._raw) {
    let item = Object.assign({}, option._raw);
    emit("select", item);
  }
};

defineExpose({
  query,
});
</script>

<style lang="less" scoped></style>
